  // import * as types from './mutation-types'
  import intelligentQualityCheck from '../../api/intelligentQualityCheck'
  import * as globalTypes from '../../mutation-types'
  import * as utils from '@/utils/m7Utils'
  // import * as types from '../quality-check/mutation-types'
  import {getCache} from '../../actions'

  /**
  * @param data
   * {
   *  name:string         任务名称 ("质检任务-2018-06-26 12:16:54")
   *  type: string        任务类型 (call、webchat)
   *  taskList: [string]  待执行列表 id数组
   *  query: { }          搜索条件 (taskList 和 query 二选一)
   *  rule:[ {}, {} ]     使用的规则列表
   * }
   *  status: string      任务状态 (ready、run、finish、fail)
   * @param session
   * @param callback
   */
  export const addTask = ({commit, state, rootState, dispatch}, data) => {
    return intelligentQualityCheck.addTask(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
  }
  /**
   * 手动评分通话
   * @param data
     * {CALL_SHEET_ID,   通话id
     *  grade_data: {
     *      qc_grade,  int  评分
     *      comment    string  备注
     *  }
     * }
     */
  export const submitOneManulQcCall = ({commit, state, rootState, dispatch}, data) => {
    return intelligentQualityCheck.submitOneManulQcCall(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
  }
  // 查询通话质检列表 参数和老质检一样
  export const getQcListCall = ({commit, state, rootState, dispatch}, data) => {
    return intelligentQualityCheck.getQcListCall(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
  }
  /**
   * 手动评分质检
   * @param data
     * {"_id":"fbce6280-7368-11e8-afd6-b78c18b6ebad",
     *  "type":"webchat",
     *  "status":"finish",
     *  "user":"1c23d",
     *  "sName":"Tom",
     *  "account":"N00000001624",
     *  "createTime":"2018-06-1910:32:17",
     *  "grade_data":{"qc_grade":81,"comment":"这个还算不错"}}
     */
  export const submitOneManulQcChat = ({commit, state, rootState, dispatch}, data) => {
    return intelligentQualityCheck.submitOneManulQcChat(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
  }
  // 查询结果全部通话条件查询表格数据
  export const queryIntelligentQualityCheckResultAllList = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultAll(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 查询全部结果通话进入详情
  export const IntelligentQualityCheckResultAllDetails = ({commit, rootState}, data) => {
    return intelligentQualityCheck.QualityCheckQuaryResultAllDetails(data).then(res => {
      if (res && res.success) {
        if (res.data === 'result_not_found') {
          return res
        } else {
          for (var i in res.data) {
            res.chatSession[i] = res.data[i]
          }
          let userPower = rootState.session.user
          let list = res.chatSession
          let p1 = _getSatisfaction({commit, state: rootState}, 'options')
          return Promise.all([p1]).then(([options]) => {
            // for (let i = 0; i < list.length; i++) {
            // 处理座席
            let userName = ''
            if (list.DISPOSAL_AGENT && list.DISPOSAL_AGENT !== '') {
              let user = utils.getCache('agents', list.DISPOSAL_AGENT)
              if (user) {
                userName = user.displayName + '[' + user.exten + ']'
              }
            }
            list.DISPOSAL_AGENT_show = userName
            // 呼叫类型
            let CONNECT_TYPE = ''
            if (utils.getCacheByKey('callType', 'code_value', list.CONNECT_TYPE) != null) {
              CONNECT_TYPE = utils.getCacheByKey('callType', 'code_value', list.CONNECT_TYPE).code_name
            }
            if (list.CONNECT_TYPE != null) {
              if (list.CONNECT_TYPE === 'dialout' || list.CONNECT_TYPE === 'dialTransfer') {
                list.CALLED_NO_SHOW = utils.checkHideTel(list.CALLED_NO, userPower)
                list.CALL_NO_SHOW = list.CALL_NO
              } else {
                list.CALLED_NO_SHOW = list.CALLED_NO
                list.CALL_NO_SHOW = utils.checkHideTel(list.CALL_NO, userPower)
              }
            }
            list.CONNECT_TYPE_show = CONNECT_TYPE
            // 接听状态
            let STATUS = ''
            if (utils.getCacheByKey('callStatus', 'code_value', list.STATUS) != null) {
              STATUS = utils.getCacheByKey('callStatus', 'code_value', list.STATUS).code_name
            }
            list.STATUS_show = STATUS
            // 满意度
            let satisfaction = ''
            let appraiseKey = list.INVESTIGATE ? list.INVESTIGATE : ''
            if (appraiseKey === '') {
              satisfaction = ''
            } else {
              for (var n = 0; n < options.length; n++) {
                if (options[n].options[0]) {
                  if (options[n].options[0].name === appraiseKey) {
                    satisfaction = options[n].name
                    break
                  }
                }
              }
            }
            if (!satisfaction) {
              satisfaction = appraiseKey || '' // 若字典配置被删除就默认显示数字eg:1
            }
            list.INVESTIGATE_show = satisfaction
            // 呼叫时长
            let chatDuration = '00:00:00'
            if (list.BEGIN_TIME && list.END_TIME) {
              chatDuration = utils.millsToDate(new Date(list.END_TIME).getTime() - new Date(list.BEGIN_TIME).getTime())
            }

            list.CALL_TIME_LENGTH_show = chatDuration
            list.queueTimeLen = utils.getCallTimeLength(list.QUEUE_TIME_LENGTH)
            // }
            return res.chatSession
          })
        }
      }
    })
  }
  // 查询结果全部在线条件查询表格数据
  export const queryIntelligentQualityCheckResultAllWebList = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultWebAll(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果通话复核
  export const queryIntelligentQualityCheckResultAllCheckResultCall = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultCallAllCheckResult(data).then(res => {
      if (res && res.success) {
        return res
      } else if (res && res.message === 'result_not_found') {
        commit(globalTypes.SET_ERROR, 'qualityCheck.result_not_found_call')
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果在线复核
  export const queryIntelligentQualityCheckResultAllCheckResultWeb = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultWebAllCheckResult(data).then(res => {
      if (res && res.success) {
        return res
      } else if (res && res.message === 'result_not_found') {
        commit(globalTypes.SET_ERROR, 'qualityCheck.result_not_found_chat')
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果全部任务列表
  export const getIntelligentQualityCheckResultAllTasklist = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckesRultAllTaskList(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果全部通话放弃质检
  export const giveUpQualityCheckesRultAllCall = ({commit, rootState}, data) => {
    return intelligentQualityCheck.giveUpQualityCheckesRultAllCall(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果全部在线放弃质检
  export const giveUpQualityCheckesRultAllWeb = ({commit, rootState}, data) => {
    return intelligentQualityCheck.giveUpQualityCheckesRultAllWeb(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 修改申诉状态为无效
  export const IntelligentQualityAppealUpdateAppealStatus = ({commit, rootState}, data) => {
    return intelligentQualityCheck.IntelligentQualityAppealUpdateAppealStatus(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
  }
  // 质检结果全部在线查详情
  export const IntelligentQualityCheckResultAllWebDetails = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultWebAllDetails(data).then(res => {
      if (res && res.success) {
        if (res.data === 'result_not_found') {
          return res
        } else {
          for (var i in res.data) {
            res.chatSession[i] = res.data[i]
          }
          let list = res.chatSession
          let platformsArr = [
            {label: utils.m7Language('webchat.websiteCon'), value: 'pc'},
            {label: utils.m7Language('webchat.wapCon'), value: 'wap'},
            {label: utils.m7Language('webchat.appCon'), value: 'sdk'},
            {label: utils.m7Language('webchat.weChatCon'), value: 'weixin'},
            {label: utils.m7Language('webchat.weChatMini'), value: 'wxmini'},
            {label: utils.m7Language('webchat.TikTok'), value: 'dy'},
            {label: utils.m7Language('webchat.platformWXKF'), value: 'wxkf'},
            {label: utils.m7Language('webchat.weBoCon'), value: 'weibo'}
          ]
          let p1 = getCache({commit, state: rootState}, {type: 'agents'})
          let p2 = getCache({commit, state: rootState}, {type: 'mailQueues'})
          let p3 = _getChannelDic({commit, state: rootState}, 'webchatCSR')
          return Promise.all([p1, p2, p3]).then(([agents, mailQueues, wechatCSR]) => {
            let userName = ''
            if (list.user && list.user !== '') {
              let user = utils.getCache('agents', list.user)
              userName = user.displayName + '[' + user.exten + ']'
            }
            list.userName = userName
            let toPeerName = ''
            if (utils.getCacheByKey('mailQueues', 'Exten', list.toPeer) != null) {
              toPeerName = utils.getCacheByKey('mailQueues', 'Exten', list.toPeer).DisplayName
            }
            list.toPeerName = toPeerName
            let satisfaction = ''
            let appraiseKey = list.appraiseKey ? list.appraiseKey : ''
            if (appraiseKey === '') {
              satisfaction = ''
            } else {
              for (var n = 0; n < wechatCSR.length; n++) {
                if (wechatCSR[n].key === appraiseKey) {
                  satisfaction = wechatCSR[n].name
                }
              }
            }
            list.satisfaction = satisfaction
            let ts = list.lastTimeStamp
            if (ts) {
              let date = new Date()
              date.setTime(ts)
              let desc = utils.getFormatDateTime(date)
              list.lastTimeDesc = desc
            }
            let chatDuration = '00:00:00'
            if (list.lastTimeDesc && list.manualTime) {
              chatDuration = utils.millsToDate(new Date(list.lastTimeDesc).getTime() - new Date(list.manualTime).getTime())
            }
            list.chatDuration = chatDuration
            if (!list.beginTime) {
              list.beginTime = ''
            }
            if (!list.totalMsgCount) {
              list.totalMsgCount = 0
            }
            if (list.sourceName && list.sName) {
              list.custName = list.sName
            }
            if (!list.sourceName) {
              list.sourceName = list.sName
            }
            if (list.firstReplyTime) {
              list.firstTime = utils.getFormatDateTime(new Date(list.firstReplyTime))
            }
            list.platformSourceName = ''
            platformsArr.forEach(item => {
              if (list.platform === item.value) {
                list.platformSourceName = item.label
              }
            })
            return res.chatSession
          })
        }
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果 任务列表  查询  = == ==
  // ======== = = == = == ==   =   = = = == = = =
  export const getQualityCheckQuaryResultQueryTasks = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultQueryTasks(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果   申诉中  start  ====
  // =========================== = == == = == = == == = == =
  //  质检结果  申诉中 查询结果
  export const getQualityCheckQuaryResultAppealList = ({commit, rootState}, data) => {
    return intelligentQualityCheck.getQualityCheckQuaryResultAppealList(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  export const rejectAppeal = ({commit, rootState}, data) => {
    return intelligentQualityCheck.rejectAppeal(data).then(res => {
      if (res && res.success) {
        return res
      }
    })
      .catch(err => {
        console.log(err)
      })
  }
  // 质检结果   申诉中  end  ======= =
  //  ===================== == = == = == == = == =
  let _getChannelDic = ({commit, state}, type) => {
    return getCache({commit, state}, {type: 'channelDic'}).then(dics => {
      let _dics = []
      for (let i = 0; i < dics.length; i++) {
        var obj = dics[i]
        if (obj.type === type) {
          _dics = obj.options || []
          break
        }
      }
      return _dics
    })
  }
  let _getSatisfaction = ({commit, state}, type) => {
    return getCache({commit, state}, {type: type}).then(dics => {
      let _dics = []
      for (let i = 0; i < dics.length; i++) {
        var obj = dics
        if (obj.name === '满意度调查选项') {
          _dics = obj.options || []
          break
        }
      }
      return _dics
    })
  }
